package com.ruigi.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruigi.Dto.SetmealDto;
import com.ruigi.pojo.Category;
import com.ruigi.pojo.R;
import com.ruigi.pojo.Setmeal;
import com.ruigi.pojo.SetmealDish;
import com.ruigi.service.CategoryService;
import com.ruigi.service.SetmealDishservice;
import com.ruigi.service.Setmealservice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealControl {
    @Autowired
    private Setmealservice setmealservice;
    @Autowired
    private SetmealDishservice setmealDishservice;
    @Autowired
    private CategoryService categoryService;

    @CacheEvict(value = "setmel", allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealservice.savemealdish(setmealDto);

        return R.success("成功返回zo茄");
    }

    @GetMapping("/page")
    public R<Page> page1(int page, int pageSize, String name) {
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        Page<SetmealDto> page2 = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealservice.page(page1, queryWrapper);
        BeanUtils.copyProperties(page1, page2, "records");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> list = records.stream().map(s -> {
            SetmealDto setmealDto = new SetmealDto();
            Long categoryId = s.getCategoryId();
            Category categoryServiceById = categoryService.getById(categoryId);
            if (categoryServiceById != null) {
                String name1 = categoryServiceById.getName();
                setmealDto.setCategoryName(name1);
                BeanUtils.copyProperties(s, setmealDto);


            }
            return setmealDto;
        }).collect(Collectors.toList());
        page2.setRecords(list);

        return R.success(page2);
    }

    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status, Long ids[]) {
        for (Long id : ids) {
            Setmeal byId = setmealservice.getById(id);
            byId.setStatus(status);
            setmealservice.updateById(byId);
        }
        return R.success("成功");

    }


    //删除
    @CacheEvict(value = "setmel", allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealservice.deleteById(ids);

        return R.success("成功");
    }

    @Cacheable(value = "setmel", key = "#setmeal.categoryId")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        List<Setmeal> list = setmealservice.list(queryWrapper);
        return R.success(list);

    }

    @GetMapping("/{id}")
    public R<SetmealDto> getbyid(@PathVariable Long id) {
        Setmeal byId = setmealservice.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(byId, setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishservice.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);

    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealservice.setmealupdate(setmealDto);
        return R.success("成功");
    }

}
